# -*- coding:utf-8 -*-
from flask_mail import Message
from app import mail , App 
from flask import render_template
from threading import Thread

def send_async_email(App,msg):
	with App.app_context():
		mail.send(msg)

def send_mail(subject,sender,recipients,text_body,html_body):
	msg = Message(subject , sender = sender , recipients = recipients)
	msg.text = text_body
	msg.html = html_body
	Thread(target = send_async_email , args  = (App,msg)).start()

def send_password_reset_email(user):
	token = user.get_reset_password_token()
	send_mail('[Microblog] Reset Your Password',
		sender = App.config['ADMINS'][0],
		recipients = [user.email] ,
		text_body = render_template('email/reset_password.txt' ,
									user = user , token  =token), 
		html_body = render_template('email/reset_password.html' ,
									user = user ,token = token))